

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/favicon.png">
  <link rel="icon" href="/img/favicon.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="Cheney">
  <meta name="keywords" content="Coding">
  
    <meta name="description" content="计算阶乘时如果直接用一个整形来存结果则容易发生溢出（由题目所给的测试数据易得，30的阶乘就已经高达三十多位），所以就有必要设计一种存储结构来存阶乘得到的值，由学过的知识容易想到可以利用链表来存储，链表的每一个节点内存储特定的几位数，由于链表是可以无限延长的 所以链表能存储的数的位数也是无限长的。">
<meta property="og:type" content="article">
<meta property="og:title" content="大数运算之计算n的阶乘 (n≥20)">
<meta property="og:url" content="https://cheney822.gitee.io/2020/12/03/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E5%AE%9E%E9%AA%8C--%E5%A4%A7%E6%95%B0%E8%BF%90%E7%AE%97%E4%B9%8B%E8%AE%A1%E7%AE%97n%E7%9A%84%E9%98%B6%E4%B9%98%20(n%E2%89%A520)/index.html">
<meta property="og:site_name" content="Cheney blog">
<meta property="og:description" content="计算阶乘时如果直接用一个整形来存结果则容易发生溢出（由题目所给的测试数据易得，30的阶乘就已经高达三十多位），所以就有必要设计一种存储结构来存阶乘得到的值，由学过的知识容易想到可以利用链表来存储，链表的每一个节点内存储特定的几位数，由于链表是可以无限延长的 所以链表能存储的数的位数也是无限长的。">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://imgbed.cheney.cc/picgo/6de9bb62ffc54602b5576e5233aa0ca1-20220405210554591.png">
<meta property="og:image" content="https://imgbed.cheney.cc/picgo/be52c037493448edbfea2b4f2c57eedc-20220405210554654.png">
<meta property="og:image" content="https://imgbed.cheney.cc/picgo/4d60bfcd3ec640549480f197750e38df-20220405210554695.png">
<meta property="og:image" content="https://imgbed.cheney.cc/picgo/408bea51b5e5485cbf1848f789ccd760-20220405210554739.png">
<meta property="og:image" content="https://imgbed.cheney.cc/picgo/879608dc1ab946ceb02e907f89c39a72-20220405210554785.png">
<meta property="og:image" content="https://imgbed.cheney.cc/picgo/8360d61c19ec4672bf3afea47b03d413-20220405210554934.png">
<meta property="article:published_time" content="2020-12-03T05:14:00.000Z">
<meta property="article:modified_time" content="2022-04-05T13:14:28.547Z">
<meta property="article:author" content="Cheney">
<meta property="article:tag" content="C++">
<meta property="article:tag" content="链表">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="https://imgbed.cheney.cc/picgo/6de9bb62ffc54602b5576e5233aa0ca1-20220405210554591.png">
  
  
  <title>大数运算之计算n的阶乘 (n≥20) - Cheney blog</title>

  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4/dist/css/bootstrap.min.css" />


  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/github-markdown-css@4/github-markdown.min.css" />
  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/hint.css@2/hint.min.css" />

  
    
    
      
      <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/highlight.js@10/styles/github-gist.min.css" />
    
  

  
    <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3/dist/jquery.fancybox.min.css" />
  


<!-- 主题依赖的图标库，不要自行修改 -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_ba1fz6golrf.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/css/main.css" />

<!-- 自定义样式保持在最底部 -->


  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    var CONFIG = {"hostname":"cheney822.gitee.io","root":"/","version":"1.8.14","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"right","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"copy_btn":true,"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":1},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/local-search.xml"};
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
<meta name="generator" content="Hexo 5.4.1"></head>


<body>
  <header style="height: 70vh;">
    <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>Cheney Blog</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

    <div class="banner" id="banner" parallax=true
         style="background: url('https://imgbed.cheney.cc/Blog_config/default.png') no-repeat center center;
           background-size: cover;">
      <div class="full-bg-img">
        <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
          <div class="page-header text-center fade-in-up">
            <span class="h2" id="subtitle" title="大数运算之计算n的阶乘 (n≥20)">
              
            </span>

            
              <div class="mt-3">
  
  
    <span class="post-meta">
      <i class="iconfont icon-date-fill" aria-hidden="true"></i>
      <time datetime="2020-12-03 13:14" pubdate>
        2020年12月3日 下午
      </time>
    </span>
  
</div>

<div class="mt-1">
  
    <span class="post-meta mr-2">
      <i class="iconfont icon-chart"></i>
      1.5k 字
    </span>
  

  
    <span class="post-meta mr-2">
      <i class="iconfont icon-clock-fill"></i>
      
      
      8 分钟
    </span>
  

  
  
</div>

            
          </div>

          
        </div>
      </div>
    </div>
  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="d-none d-lg-block col-lg-2"></div>
    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div class="py-5" id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">大数运算之计算n的阶乘 (n≥20)</h1>
            
            <div class="markdown-body">
              <h1 id="一、问题描述"><a href="#一、问题描述" class="headerlink" title="一、问题描述"></a>一、问题描述</h1><p>大数运算——计算n的阶乘 (n≥20)。</p>
<h2 id="【基本要求】"><a href="#【基本要求】" class="headerlink" title="【基本要求】"></a>【基本要求】</h2><ul>
<li>（1）数据的表示和存储：<br>①累积运算的中间结果和最终的计算结果的数据类型要求是整型——这是问题本身的要求。<br>②试设计合适的存储结构，要求每个元素或结点最多存储数据的3位数值。</li>
<li>（2）数据的操作及其实现：基于设计的存储结构实现乘法操作，要求从键盘上输入n值；在屏幕上显示最终计算结果。【测试数据】<br>（1）n=20，n!=2432902008176640000<br>（2）n=30，n!=265252859812191058636308480000000，</li>
</ul>
<h2 id="【实现提示】"><a href="#【实现提示】" class="headerlink" title="【实现提示】"></a>【实现提示】</h2><ul>
<li>（1）设计数据的存储结构：<br>介于阶乘运算的精确性以及实型数据表示的不精确性，本题不能采用实型表示累积运算的中间结果和最终的计算结果，而只能用整型。然而由于普通整型和长整型所能表述数的范围受其字长的限制，不能表示大数阶乘的累积结果，故必须设计一个合适的数据结构实现对数据的存储，例如可以让每个元素或结点存储数据的若干位数值。从问题描述不难看出n值为任意值，故为使程序尽量不受限制，应采用动态存储结构。</li>
<li>（2）实现两个数的乘法运算需考虑：乘数的各位数都要与被乘数进行乘法运算；乘法过程中的进位问题及其实现。因每个元素或结点最多存储数据的3位数值，故当元素或结点中的数值大于999，需向前一个元素或结点进位。</li>
</ul>
<h1 id="二、需求分析"><a href="#二、需求分析" class="headerlink" title="二、需求分析"></a>二、需求分析</h1><p>计算阶乘时如果直接用一个整形来存结果则容易发生溢出（由题目所给的测试数据易得，30的阶乘就已经高达三十多位），所以就有必要设计一种存储结构来存阶乘得到的值，由学过的知识容易想到可以利用链表来存储，链表的每一个节点内存储特定的几位数，由于链表是可以无限延长的 所以链表能存储的数的位数也是无限长的。<br>所以现在我们设计一个程序，由用户输入其需要计算的阶乘的数字，程序计算出的阶乘的结果对应的呈现在屏幕上。</p>
<h1 id="三、设计"><a href="#三、设计" class="headerlink" title="三、设计"></a>三、设计</h1><h2 id="3-1-整体设计"><a href="#3-1-整体设计" class="headerlink" title="3.1 整体设计"></a>3.1 整体设计</h2><p>用struct设计节点类方便访问，再设计链表类定义数据成员和构造函数并声明打印函数和阶乘函数为其友元函数，定义阶乘函数和打印函数用来计算阶乘</p>
<p> <img src="https://imgbed.cheney.cc/picgo/6de9bb62ffc54602b5576e5233aa0ca1-20220405210554591.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<h2 id="3-2-详细设计"><a href="#3-2-详细设计" class="headerlink" title="3.2 详细设计"></a>3.2 详细设计</h2><ul>
<li>节点类：用struct设计节点类，设计数据域data和指针域link，设计两个构造函数，一个无参构造函数初始指针域为NULL，一个含一个变量a的构造函数初始指针域为NULL数据域为a。<br>链表类：设计一个数据成员为节点类类型的指针，设计两个构造函数</li>
<li>阶乘函数：<br>i从一到二十逐步加一，C指针指向链表头节点，进位表示为Out<br>如果C指针不为空则循环以下语句<br>节点新的值=旧的值*i + 进位<br>进位=当前节点值/1000 （高位的1代表低位的1000）<br>当前值对1000取余（保证只存三位数，剩下的值由Out传给高位）<br>在当前节点处理完后 判断是否需要增加节点<br>（进位不为零且后继为空则创建一个后继节点）<br>C指针向后指一位</li>
<li>打印函数：<br>如果当前指针指向节点的指针域为空，则打印此节点的数据<br>否则1，打印后继节点 2，输出当前节点数据（位宽为三，空位补0）</li>
</ul>
<h1 id="四、测试数据及测试结果："><a href="#四、测试数据及测试结果：" class="headerlink" title="四、测试数据及测试结果："></a>四、测试数据及测试结果：</h1><p>测试输入：-5，20，30，1000<br>测试目的：设计该输入的目的在于测试程序在哪方面可能存在漏洞；</p>
<p>4.1：-5的计算<br>正确输出：输入有误，请重新输入一个自然数<br>实际输出：输入有误，请重新输入一个自然数<br> <img src="https://imgbed.cheney.cc/picgo/be52c037493448edbfea2b4f2c57eedc-20220405210554654.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<p>4.2： 0的计算<br>正确输出：1<br>实际输出：1<br> <img src="https://imgbed.cheney.cc/picgo/4d60bfcd3ec640549480f197750e38df-20220405210554695.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<p>4.3：20的计算<br>正确输出：2432902008176640000<br>实际输出：2432902008176640000</p>
<p><img src="https://imgbed.cheney.cc/picgo/408bea51b5e5485cbf1848f789ccd760-20220405210554739.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<p>4.4：30的计算<br>正确输出：265252859812191058636308480000000<br>实际输出：265252859812191058636308480000000<br> <img src="https://imgbed.cheney.cc/picgo/879608dc1ab946ceb02e907f89c39a72-20220405210554785.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<p>4.5：1000的计算<br>输出结果较长，此处省略<br>且经对比实际输出=正确输出</p>
<p><img src="https://imgbed.cheney.cc/picgo/8360d61c19ec4672bf3afea47b03d413-20220405210554934.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>

            </div>
            <hr>
            <div>
              <div class="post-metas mb-3">
                
                  <div class="post-meta mr-3">
                    <i class="iconfont icon-category"></i>
                    
                      <a class="hover-with-bg" href="/categories/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/">数据结构</a>
                    
                  </div>
                
                
                  <div class="post-meta">
                    <i class="iconfont icon-tags"></i>
                    
                      <a class="hover-with-bg" href="/tags/C/">C++</a>
                    
                      <a class="hover-with-bg" href="/tags/%E9%93%BE%E8%A1%A8/">链表</a>
                    
                  </div>
                
              </div>
              
                <p class="note note-warning">
                  
                    本博客所有文章除特别声明外，均采用 <a target="_blank" href="https://creativecommons.org/licenses/by-sa/4.0/deed.zh" rel="nofollow noopener noopener">CC BY-SA 4.0 协议</a> ，转载请注明出处！
                  
                </p>
              
              
                <div class="post-prevnext">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2020/12/04/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E5%AE%9E%E9%AA%8C--%E5%B8%A6%E7%8E%AF%E3%80%81%E7%9B%B8%E4%BA%A4%E9%93%BE%E8%A1%A8%E9%97%AE%E9%A2%98/">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">带环、相交链表问题</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2020/12/02/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E5%AE%9E%E9%AA%8C--%E8%A1%A8%E8%BE%BE%E5%BC%8F%E7%9A%84%E5%90%8E%E7%BC%80%E8%A1%A8%E7%A4%BA/">
                        <span class="hidden-mobile">表达式的后缀表示</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>
    
      <div class="d-none d-lg-block col-lg-2 toc-container" id="toc-ctn">
        <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>

      </div>
    
  </div>
</div>

<!-- Custom -->


    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v"
                 for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>
    

    
  </main>

  <footer class="text-center mt-5 py-3">
  <div class="footer-content">
     <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://cheney822.gitee.io/" target="_blank" rel="nofollow noopener"><span>备用网址</span></a> 
  </div>
  

  
  <!-- 备案信息 -->
  <div class="beian">
    <span>
      <a href="http://beian.miit.gov.cn/" target="_blank" rel="nofollow noopener">
        皖ICP备2022002876号-1
      </a>
    </span>
    
  </div>


  
</footer>


  <!-- SCRIPTS -->
  
  <script  src="https://cdn.jsdelivr.net/npm/nprogress@0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/nprogress@0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://cdn.jsdelivr.net/npm/jquery@3/dist/jquery.min.js" ></script>
<script  src="https://cdn.jsdelivr.net/npm/bootstrap@4/dist/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>

<!-- Plugins -->


  <script  src="/js/local-search.js" ></script>



  
    <script  src="/js/img-lazyload.js" ></script>
  



  



  
    <script  src="https://cdn.jsdelivr.net/npm/tocbot@4/dist/tocbot.min.js" ></script>
  
  
    <script  src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3/dist/jquery.fancybox.min.js" ></script>
  
  
    <script  src="https://cdn.jsdelivr.net/npm/anchor-js@4/anchor.min.js" ></script>
  
  
    <script defer src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js" ></script>
  






  <script  src="https://cdn.jsdelivr.net/npm/typed.js@2/lib/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var title = document.getElementById('subtitle').title;
      
        typing(title);
      
    })(window, document);
  </script>















<!-- 主题的启动项 保持在最底部 -->
<script  src="/js/boot.js" ></script>


</body>
</html>
